package com.yida.gwj.basis.controller;

import com.yida.common.core.constant.SecurityConstants;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.api.service.RemoteGwjConfigService;
import com.yida.gwj.basis.domain.GwjInitBusiness;
import com.yida.gwj.basis.domain.GwjInitBusinessLog;
import com.yida.gwj.basis.domain.dto.GwjInitBusinessDTO;
import com.yida.gwj.basis.domain.dto.GwjInitBusinessLogDTO;
import com.yida.gwj.basis.domain.request.GwjInitBusinessSaveSubmitRequest;
import com.yida.gwj.basis.enums.GwjInitBusinessCommunityStatus;
import com.yida.gwj.basis.service.IGwjInitBusinessCommunityService;
import com.yida.gwj.basis.service.IGwjInitBusinessLogService;
import com.yida.gwj.basis.service.IGwjInitBusinessService;
import com.yida.gwj.basis.service.IGwjInitSplitBusinessService;
import com.yida.gwj.common.domain.dto.GwjBusinessFlagDTO;
import com.yida.gwj.common.domain.dto.GwjBusinessStepDTO;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.utils.GwjConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 小区拆分（基础数据管理模块）Controller
 *
 * @author lqq
 * @date 2022-11-15
 */
@RestController
@RequestMapping("/split")
public class GwjInitSplitBusinessController extends BaseController {

	@Autowired
	private IGwjInitBusinessService businessService;

	@Autowired
	private IGwjInitBusinessLogService logService;

	@Autowired
	private IGwjInitSplitBusinessService splitBusinessService;

	@Autowired
	private IGwjInitBusinessCommunityService communityService;

	@Autowired
	private RemoteGwjConfigService remoteGwjConfigService;

	/**
	 * 查询业务列表
	 */
	@GetMapping("/list")
	@RequiresPermissions("gwj:basis:split:list")
	public TableDataInfo list(@Validated GwjInitBusiness business) {
		startPage();
		business.setBizFlagId(GwjBusinessFlagType.SPLIT.getCode());
		List<GwjInitBusiness> list = businessService.getList(business);
		return GwjConvertUtil.convertTable(getDataTable(list), GwjInitBusinessDTO.class);
	}

	/**
	 * 查询业务列表
	 */
	@GetMapping("/info")
	@RequiresPermissions("gwj:basis:split:list")
	public AjaxResult getBusinessInfo() {
		AjaxResult ajax = AjaxResult.success();
		ajax.put("bizFlag", GwjConvertUtil.convert(remoteGwjConfigService.selectGwjConfBusinessFlagByBizFlagId(GwjBusinessFlagType.SPLIT.getCode(), SecurityConstants.INNER).getData(), GwjBusinessFlagDTO.class));
		List<GwjBusinessStepDTO> steps = GwjConvertUtil.convertList(remoteGwjConfigService.selectGwjConfStepListByFlagId(GwjBusinessFlagType.SPLIT.getCode(),SecurityConstants.INNER).getData(), GwjBusinessStepDTO.class);
		steps.forEach(stepDTO -> stepDTO.setConfigs(remoteGwjConfigService.selectGwjConfStepConfigInfoCodeListByBizFlagId(stepDTO.getStepId(),SecurityConstants.INNER).getData()));
		ajax.put("steps", steps);
		return ajax;
	}

	/**
	 * 导出业务列表
	 */
	@PostMapping("/export")
	@RequiresPermissions("gwj:basis:split:export")
	@Log(title = "小区拆分-导出（基础数据管理模块）", businessType = BusinessType.EXPORT, systemName = "gwj")
	public void export(HttpServletResponse response, GwjInitBusiness business) {
		business.setBizFlagId(GwjBusinessFlagType.SPLIT.getCode());
		business.setStatus(GwjInitBusinessCommunityStatus.SPLIT.getCode());
		List<GwjInitBusiness> list = businessService.getList(business);
		ExcelUtil<GwjInitBusiness> util = new ExcelUtil<>(GwjInitBusiness.class);
		util.exportExcel(response, list, "业务（基础数据管理模块）数据");
	}

	/**
	 * 送件
	 */
	@PostMapping("/send")
	@RequiresPermissions("gwj:basis:split:send")
	@Log(title = "小区拆分-送件（基础数据管理模块）", systemName = "gwj")
	public AjaxResult sendBusiness(@Validated(GwjInitBusinessLog.Send.class) @RequestBody GwjInitBusinessLog businessLog) {
		businessService.send(businessLog);
		return AjaxResult.success();
	}

	/**
	 * 回退
	 */
	@DeleteMapping("/fallback")
	@RequiresPermissions("gwj:basis:split:fallback")
	@Log(title = "小区拆分-回退（基础数据管理模块）", systemName = "gwj")
	public AjaxResult fallback(@Validated(GwjInitBusinessLog.Fallback.class) @RequestBody GwjInitBusinessLog businessLog) {
		businessService.fallback(businessLog);
		return AjaxResult.success();
	}

	/**
	 * 签署意见
	 */
	@PostMapping("/signOpinion")
	@RequiresPermissions("gwj:basis:split:signOpinion")
	@Log(title = "小区拆分-签署意见（基础数据管理模块）", systemName = "gwj")
	public AjaxResult signOpinion(@Validated(GwjInitBusinessLog.Sign.class) @RequestBody GwjInitBusinessLog businessLog) {
		businessService.signOpinion(businessLog);
		return AjaxResult.success();
	}

	/**
	 * 查看业务流程
	 */
	@GetMapping("/flow")
	@RequiresPermissions("gwj:basis:split:log")
	public AjaxResult flow(Long bizId) {
		return AjaxResult.success(businessService.getBusinessFlow(bizId, GwjBusinessFlagType.SPLIT.getCode()));
	}

	/**
	 * 查看业务日志
	 */
	@GetMapping("/log/{bizId}")
	@RequiresPermissions("gwj:basis:split:log")
	public AjaxResult log(@PathVariable Long bizId) {
		return AjaxResult.success(GwjConvertUtil.convertList(logService.getLogList(bizId), GwjInitBusinessLogDTO.class));
	}

	/**
	 * 保存
	 */
	@PostMapping("/save")
	@RequiresPermissions("gwj:basis:split:save")
	@Log(title = "小区拆分-保存（基础数据管理模块）", systemName = "gwj")
	public AjaxResult save(@Validated(GwjInitBusinessSaveSubmitRequest.Split.class) @RequestBody GwjInitBusinessSaveSubmitRequest saveSubmitRequest) {
		return AjaxResult.success(splitBusinessService.saveBusiness(saveSubmitRequest));
	}

	/**
	 * 修改保存
	 */
	@PutMapping("/update")
	@RequiresPermissions("gwj:basis:split:save")
	@Log(title = "小区拆分-修改保存（基础数据管理模块）", systemName = "gwj")
	public AjaxResult update(@Validated(GwjInitBusinessSaveSubmitRequest.Split.class) @RequestBody GwjInitBusinessSaveSubmitRequest saveSubmitRequest) {
		splitBusinessService.updateBusiness(saveSubmitRequest);
		return AjaxResult.success();
	}

	/**
	 * 提交
	 */
	@PostMapping("/submit")
	@RequiresPermissions("gwj:basis:split:submit")
	@Log(title = "小区拆分-提交（基础数据管理模块）", systemName = "gwj")
	public AjaxResult submit(@Validated @RequestBody GwjInitBusinessSaveSubmitRequest saveSubmitRequest) {
		splitBusinessService.submitBusiness(saveSubmitRequest);
		return AjaxResult.success();
	}

	/**
	 * 案例信息
	 */
	@GetMapping("/caseInfo/{bizId}")
	@RequiresPermissions("gwj:basis:split:list")
	public AjaxResult caseInfo(@PathVariable Long bizId) {
		return AjaxResult.success(businessService.caseInfo(bizId));
	}

	/**
	 * 小区信息
	 */
	@GetMapping("/communityInfo/{bizId}")
	@RequiresPermissions("gwj:basis:split:list")
	public AjaxResult communityInfo(@PathVariable Long bizId) {
		return AjaxResult.success(communityService.getList(bizId));
	}

	/**
	 * 删页
	 */
	@DeleteMapping("/delete/{bizId}")
	@Log(title = "小区拆分-删页（基础数据管理模块）", systemName = "gwj")
	public AjaxResult deleteBusiness(@PathVariable Long bizId) {
		businessService.deleteByBizId(bizId);
		return AjaxResult.success();
	}

	/**
	 * 小区信息
	 */
	@GetMapping("/showBuild/{bizId}")
	@RequiresPermissions("gwj:basis:split:list")
	public AjaxResult showBuild(@PathVariable Long bizId) {
		return AjaxResult.success(communityService.getList(bizId));
	}
}
